package com.nike.amoeba.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.nike.amoeba.base.MyBaseViewModel
import com.nike.amoeba.bean.AppRoomDataBase
import com.nike.amoeba.bean.db.AssetBean
import com.nike.amoeba.bean.db.TaskAssetBean
import com.nike.amoeba.bean.db.TaskStockBean
import com.nike.amoeba.ext.INVENTORY_STOCK
import com.nike.amoeba.util.CacheUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import java.util.Collections

/**
 * @Author nike
 * @Date 2023/7/7 12:00
 * @Description
 */
class TaskModel: MyBaseViewModel() {

    var listTaskAssetBean: MutableLiveData<ArrayList<TaskAssetBean>> = MutableLiveData()

    var listTaskStockBean: MutableLiveData<ArrayList<TaskStockBean>> = MutableLiveData()

    fun onRequest(code: String?) {
        viewModelScope.launch(Dispatchers.IO) {
            val list = taskAssetDao.findById(userId, companyId, code)
            list.forEach {
                var list = assetDao.findById(userId, it.Stocktakeno, INVENTORY_STOCK, companyId, code) as ArrayList<AssetBean>
                it.Progress = list.size
            }
            Collections.reverse(list);
            listTaskAssetBean.postValue(list as ArrayList<TaskAssetBean>)
        }
    }

    fun onStockTakeList(code: String?) {
        viewModelScope.launch(Dispatchers.IO) {
            val list = taskStockDao.findById(userId, companyId, code)
            list.forEach {
                var list = stockEpcDao.findOrderIdAll(it.OrderNo)
                val split = it.Inventory_progress?.split("/")
                it.Inventory_progress = "${list.size}" + "/" + (split?.get(1) ?: "")
            }
            listTaskStockBean.postValue(list as ArrayList<TaskStockBean>)
        }
    }

}